package kaist.sep521.lego.circlebicycle;

import java.io.IOException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

class PollingThread extends Thread {
	private static boolean isRunning;
	private RunActivity runActivity = null;

	public PollingThread(RunActivity ra) {
		this.runActivity = ra;
		isRunning = true;
	}

	public void run() {
		int i = 0;
		Log.d("ashton", "Thread start");

		while (isRunning) {

			if (MainActivity.testModeFlag == true) {
				int status = MainActivity.myInfo.getStatus();
				if ( status != 1 && status != 2){
					getTestLocation(i++);
					
					runActivity.location_update(MainActivity.myInfo);
				}
			}

			//Log.d("ashton", "i=" + i++ + " thread is running!");
			//runActivity.parseUserList(MainActivity.myInfo.getGroup());
			runActivity.runOnUiThread();
			runActivity.checkVoiceMSG();

			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				System.out.println(e);
			}
		}
	}

	public void getTestLocation(int list) {

		try {
			URL url = new URL("http://14.63.196.253/~lego/cctest_loc.php?list="+list);

			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
			XmlPullParser parser = parserCreator.newPullParser();

			parser.setInput(url.openStream(), null);

			String s_horizontal = null;
			String s_vertical = null;

			//int parserEvent = parser.getEventType();
			while (parser.next() != XmlPullParser.END_TAG) {
				if (parser.getEventType() != XmlPullParser.START_TAG) {
					continue;
				}
				String name = parser.getName();
				if (name.equals("horizontal")) {
					s_horizontal = readHori(parser);
				} else if (name.equals("vertical")) {
					s_vertical = readVerti(parser);
				} 
			}

			Log.e("Responce-->","Horizontal = "+s_horizontal+" Vertical = "+ s_vertical);

			Double hori = Double.parseDouble(s_horizontal);
			Double verti = Double.parseDouble(s_vertical);

			MainActivity.myInfo.setLatitude(hori);
			MainActivity.myInfo.setLongitude(verti);

			// parserEvent = parser.next();
			// }
		} catch(Exception e){
			Log.e("Net", "Netwrok Connection Error", e);
		}
	}


	// Processes title tags in the feed.
	private String readHori(XmlPullParser parser) throws IOException, XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, null, "horizontal");
		String horizontal = readText(parser);
		parser.require(XmlPullParser.END_TAG, null, "horizontal");
		return horizontal;
	}

	// Processes title tags in the feed.
	private String readVerti(XmlPullParser parser) throws IOException, XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, null, "vertical");
		String vertical = readText(parser);
		parser.require(XmlPullParser.END_TAG, null, "vertical");
		return vertical;
	}

	// For the tags title and summary, extracts their text values.
	private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
		String result = "";
		if (parser.next() == XmlPullParser.TEXT) {
			result = parser.getText();
			parser.nextTag();
		}
		return result;
	}

	public static void setRunningState(boolean state) {
		isRunning = state;
	}	
}
